
import { WeApp } from '../../../common/common';
import { config } from '../../../etc/config';
import * as __ from '../../../assets/plugins/underscore.js';
import httpClient from '../../../common/http-client';
import { GoodsNav } from '../../../model/nav';
import { Goods } from '../../../model/goods';
import tools from '../../../common/tools';
import wxService from '../../../common/wxService';
import { prompt } from '../../../assets/components/wux';
import utils from '../../../utils/utils'

const app = getApp();
interface GoodsListPageData {
	isTop?: boolean,
	hidden: boolean,
	goods: Array<any>,
	searchParams: GoodsSearchParam,
	searchDialogHidden?: boolean,
	categoryDialogHidden?: boolean,
	pageData?: any,
	goodsFilters?: Array<{
		key?: string,
		values?: Array<string>
	}>,
	categoryFilters?: Array<{
		id?: string,
		text?: string,
		extend?: boolean,
		value?: Array<{
			key?: string,
			values?: Array<string>
		}>
	}>,
	hasFilters?: boolean,
	selectFilters?: any,
	hasNext?: boolean,
	hasRefesh?: boolean,
	sortValue?:number
}

interface GoodsSearchParam {
	key?: string,
	nid?: string,
	bid?: string,
	label?: string,
	cid?: string,
	filters?: {},
	i?: number,
	o?: number,//0：（默认）综合排序  1：价格  2:销量
	desc?: number,
}

interface GoodsListPage extends IPage {
}

class GoodsListPage {
	private searchParams: GoodsSearchParam;
	public data: GoodsListPageData = {
		isTop: true,
		hidden: !0,
		goods: [],
		searchParams: {},
		hasFilters: true,
		sortValue:0
	}

	public onLoad(option: GoodsSearchParam): void {
		this.searchParams = {
			nid: option.nid,
			bid: option.bid,
			cid: option.cid,
			label: option.label,
			key: option.key != undefined ? decodeURIComponent(option.key) : null,
			i: 1,
			o: 0,
			filters: {},
			desc: 1 
		};
		wxService.showLoading();
		setTimeout(() => {
			this.getGoods(this.searchParams);
			const sysInfo: wx.GetSystemInfoResult = wx.getSystemInfoSync();
			this.setData({
				'deviceHeight': sysInfo.windowHeight || 750,
				'searchParams': this.searchParams
			});
		}, 0);
	}


	getGoods(param: GoodsSearchParam) {
		let url = config.goods.goods_list;
		wx.showNavigationBarLoading();
		httpClient.post<Goods>(url, param)
			.then((res) => {
				if (res.succeed) {
					var originGoods = this.data.goods;
					if (!res.pageData.total) {
						prompt.init('msg1', {
							title: '',
							text: '暂时没有相关数据!',
							icon:'http://r.59im.com/wx/tips/lack_without_produe_icon@2x.png'
						}).show();
					}
					if (res.pageData.datas.length > 0) {
						__.forEach(res.pageData.datas, (data) => {
							originGoods.push(data);
						});
					}
					res.pageData.datas = [];
					let goodsFilters = this.data.goodsFilters;
					let data = {
						'goods': originGoods,
						'hasNext': res.pageData.hasNext,
						'hasRefesh': false
					};					
					this.setData(data);
				} else {
					utils.showToastErr(res.msg);
				}
				wx.stopPullDownRefresh();
				utils.closeAllLoading();
			}).catch(e => {
				utils.closeAllLoading();
				utils.showToastErr('加载失败');
			});
	}

	goodsDialogToggleEventHandler() {
		this.setData({
			'searchDialogHidden': !this.data.searchDialogHidden,
			'categoryDialogHidden': false
		})
	}

	clearGoods() {
		this.setData({
			"goods": []
		})
	}

	// 上拉加载更多
	loadMore() {
		if (!this.data.hasNext) {
			return;
		}
		var self = this;
		setTimeout(function () {
			var param = self.data.searchParams;
			param.i++;
			self.getGoods(param);
		}, 1);
	}


	// 下拉刷新
	refresh(e) {
		if (this.data.hasRefesh) {
			return;
		}
		this.setData({
			'hasRefesh': true,
		});
		var self = this;
		setTimeout(function () {
			var param = self.data.searchParams;
			param.i = 1;
			self.clearGoods();
			self.getGoods(param);
		}, 1);
	}

}

Page(new GoodsListPage());
